<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<mate http-equiv="X-UA-Compatible" content="IE=edge">	
		<mate name="viewport" content="width=device-width,initial-scale=1.0">
		<title>自定义对象</title>
	</head>
	
	<body>
		<script>
			//1.直接创建模式
//			var person = new Object();
//			person.name = "Lily";
//			person.age = 19;
//			person.speak = function(){
//				alert(this.name + " is " + this.age + "yeats old");
//			}
//			person.speak();
			//2.工厂模式
//			function createPerson(name,age){
//				var person = new Object();
//				person.name = name;
//				person.age = age;
//				person.speak = function(){
//					alert(this.name + " is " + this.age + "yeats old");
//				}
//				return person;
//			}
//			var person1 = createPerson("Lily ",19)//由对象构造器创建新的对象实例
//			person1.speak();
			//3.构造函数模式
//			function Person(name1,age1){//构造函数的名字必须和类名一致,类名首字母必须为大写字母
//				this.name = name1;
//				this.age = age1;
//				this.speak = function () {
//				 	alert(this.name + " is " + this.age + "yeats old");
//				}
//			}
//			var person1 = new Person("Lily ",19);
//			var person2 = new Person("Jack ",18);
//			console.log(person2 instanceof Person);//true,判断 person2 是否是Person的实例
			//4.原型模式
//			function Person() { }
//			Person.prototype.name = "Lily";
//			Person.prototype.age = 19;
//			Person.prototype.speak = function () {
//				alert(this.name + " is " + this.age + "yeats old");
//			}
//			var person1 = new Person();
//			person1.speak();
			//5.混合模式（原型模式 + 构造函数模式）
//			function Person(name,age){
//				this.name = name;
//				this.age = age;
//			}
//			Person.prototype.speak = function(){
//				alert(this.name + " is " + this.age + "yeats old");
//			}
//			Person.prototype.sports() = function(){
//				
//			}
//			var person1 = new Person();
//			person1.speak();
			//6.ES6方法创建类和对象
//			class Person{
//				constructor(name,age){
//					this.name = name;
//					this.age = age;
//				}
//				speak(){
//					alert(this.name + " is " + this.age + "yeats old");
//				}
//			}
//			var person1 = new Person("lucy",20);
//			person1.speak();
			//7.ES7 创建类和对象
//			class Person{
//				name="lucy";
//				age=19;
//				speak(){
//					alert(this.name + " is " + this.age + "yeats old");
//				}
//			}
//			var person2=new Person();
//			person2.speak();
			//8.练习1		
//			function Student(Id,name,c,js){
//				this.Id=Id;
//				this.name=name;
//				this.c=parseFloat(c);
//				this.js=parseFloat(js);
//			}
//			Student.prototype.summation = function(){
//				return this.c+whis.js;
//			}
//			Stu1 = new Student(35171106,'小武',80,81);
//			Stu2 = new Student(35171107,'小懿',85,86);
//			console.log(.getElementById('info').innerHTML =
//			stu1.Id + sut1.name + "的科目总成绩是" + sut1.summation() + "<br>"
//			+ stu2.Id + sut2.name + "的科目总成绩是" + sut2.summation());

			//9.练习2 矩形对象的建立
			function Student(width,height){	
				this.width = parseFloat(width);
				this.height = parseFloat(height);
			}
			Student.prototype.getArea = function() {
				return this.width * this.height;
			}
			Student.prototype.getPreimeter = function() {
				return this.width + this.height;
			}
			var num = new Student(20,30);
			console.log("这个矩形的面积为："+num.getArea()+"，周长为"+num.getPreimeter())
		</script>
	</body>
</html>
